package com.zrgj.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zrgj.common.response.BaseResponse;
import com.zrgj.common.response.StatusCodeEnum;
import com.zrgj.domain.Product;
import com.zrgj.domain.ProductPo;
import com.zrgj.domain.ShoppingCar;
import com.zrgj.domain.User;
import com.zrgj.manager.service.IProductService;
import com.zrgj.manager.service.IShoppingCarService;
import com.zrgj.manager.service.IUserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/cart")
public class CartController {
    @Reference
    IShoppingCarService shoppingCarService;
    @Reference
    IUserService userService;
    @Reference
    IProductService productService;

    @RequestMapping(value = "/getCartList")
    public BaseResponse getCartList(String userName){

        User user = userService.findByName(userName);
        if (user == null){
            return new BaseResponse(StatusCodeEnum.NOT_LOGIN,"user_loginUI");
        }else {
            List<ShoppingCar> cartList = shoppingCarService.selectByUser(user.getUserId());
            return new BaseResponse(StatusCodeEnum.SUCCESS,cartList);
        }
    }

    @RequestMapping("/cartUI")
    public BaseResponse cartUI(){
        return new BaseResponse(StatusCodeEnum.SUCCESS,"user_cart");
    }

    @RequestMapping(value = "/addCart")
    public BaseResponse addCart(String userName,Integer productId, @RequestParam(required = false,defaultValue = "1") Integer count){
        User user = userService.findByName(userName);
        if (user == null){
            return new BaseResponse(StatusCodeEnum.NOT_LOGIN,"user_login");
        }

        Product keyProduct = new Product();
        keyProduct.setProductId(productId);
        Product product = productService.selectPro(keyProduct).get(0);

        ShoppingCar cart = new ShoppingCar();

        cart.setUserId(user.getUserId());
        cart.setProductId(productId);
        cart.setCarCount(count);

        shoppingCarService.insert(cart);

        cart.setProduct(product);
        cart.setUser(user);

        return new BaseResponse(StatusCodeEnum.SUCCESS,cart);
    }

    @RequestMapping(value = "/update")
    public BaseResponse update(@RequestBody ShoppingCar cart){
        shoppingCarService.update(cart);
        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "/delete")
    public BaseResponse delete(Integer carId){
        shoppingCarService.delete(carId);
        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

}
